package com.movitech.mobile.cz.base.bizconfig.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.movitech.mobile.cz.base.bizconfig.entity.SStrategicCategory;
import com.movitech.mobile.cz.base.bizconfig.entity.ext.ExtSStrategicCategory;
import com.movitech.mobile.cz.base.bizconfig.entity.ext.SStrategicCategoryReqVO;
import com.movitech.mobile.cz.base.bizconfig.mapper.SStrategicCategoryMapper;
import com.movitech.mobile.cz.base.bizconfig.mapper.ext.ExtSStrategicCategoryMapper;
import com.movitech.mobile.cz.base.bizconfig.service.SStrategicCategoryService;
import com.movitech.mobile.cz.base.common.aop.ServiceThrowable;
import com.movitech.mobile.cz.base.common.license.LicenseUtil;
import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.common.util.DateUtils;
import com.movitech.mobile.cz.base.common.util.PageCode;
import com.movitech.mobile.cz.base.common.util.StringUtils;
import com.movitech.mobile.cz.base.common.utils.IdGen;
import com.movitech.mobile.cz.base.modules.controller.RestReturnResult;
import com.movitech.mobile.cz.base.util.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 战略需求分类（战略采购分类） 服务实现类
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-02-28
 */
@Service("sStrategicCategoryService")
@Transactional
public class SStrategicCategoryServiceImpl extends ServiceImpl<SStrategicCategoryMapper, SStrategicCategory> implements SStrategicCategoryService {

    @Autowired
    private ExtSStrategicCategoryMapper extSStrategicCategoryMapper;

    @Override
    public SStrategicCategory insertOrUpdateSStrategicCategory(SStrategicCategory sStrategicCategory) {
        boolean checkNeedFlag = true;
        LicenseUtil.INSTANCE.validate();
        if(StringUtils.isNotEmpty(sStrategicCategory.getId())){//update
            SStrategicCategory sStrategicCategoryDb = this.selectById(sStrategicCategory.getId());
            if(sStrategicCategoryDb.getCategoryName().equals(sStrategicCategory.getCategoryName())){
                checkNeedFlag = false;
            }
            sStrategicCategory.setBaseFeilds(UserUtils.getCurrentUserId(),null,CommonConstants.DEL_FLAG_NORMAL,false);
        }else{
            sStrategicCategory.setCategoryCode(DateUtils.formatDate(DateUtils.getNow(),DateUtils.TIME_STAMP));
            sStrategicCategory.setId(IdGen.uuid());
            sStrategicCategory.setBaseFeilds(UserUtils.getCurrentUserId(),null,CommonConstants.DEL_FLAG_NORMAL,true);
        }
        if(checkNeedFlag){
            List<ExtSStrategicCategory> ls=extSStrategicCategoryMapper.checkName(sStrategicCategory);
            if(ls!=null&&ls.size()>0){
                throw new RuntimeException("名称重复",new ServiceThrowable());
            }
        }

       if(this.insertOrUpdate(sStrategicCategory)){
            return sStrategicCategory;
       }else{
           return null;
       }
    }

    @Override
    public List<ExtSStrategicCategory> getSpecialityList() {
        LicenseUtil.INSTANCE.validate();
        List<ExtSStrategicCategory>  ls=extSStrategicCategoryMapper.getSpecialityList();
        return ls;
    }

    @Override
    public void deleteById(String id) {
        SStrategicCategory sStrategicCategory= this.getSStrategicCategoryById(id);
        if(sStrategicCategory!=null){
//            sStrategicCategory.setDelFlag(CommonConstants.DEL_FLAG_DELETE);
            sStrategicCategory.setBaseFeilds(UserUtils.getCurrentUserId(),null,CommonConstants.DEL_FLAG_DELETE,false);
            this.updateById(sStrategicCategory);
        }
    }

    @Override
    public SStrategicCategory getSStrategicCategoryById(String id) {
        SStrategicCategory sStrategicCategory=this.selectById(id);
        if(sStrategicCategory.getDelFlag()==CommonConstants.DEL_FLAG_NORMAL){
            return sStrategicCategory;
        }
        return null;
    }

    @Override
    public void updateStatus(String id) {
        LicenseUtil.INSTANCE.validate();
        SStrategicCategory sStrategicCategor= this.getSStrategicCategoryById(id);
        if(sStrategicCategor!=null){
            SStrategicCategory sStrategicCategoryUpdate = new SStrategicCategory();
            sStrategicCategoryUpdate.setId(id);
            if(sStrategicCategor.getCategoryStatus()==CommonConstants.YES_INTEGER){
                sStrategicCategoryUpdate.setCategoryStatus(CommonConstants.NO_INTEGER);
            }else{
                sStrategicCategoryUpdate.setCategoryStatus(CommonConstants.YES_INTEGER);
            }
            updateById(sStrategicCategoryUpdate);
            //insertOrUpdateSStrategicCategory(sStrategicCategor);

        }else{
            throw new RuntimeException("数据不存在",new ServiceThrowable());
        }
    }

    /**
     * 根据战略专业id查询战略品类
     */
    @Override
    public List<ExtSStrategicCategory> getCategoryList(String parentId) {
        List<ExtSStrategicCategory>  ls=extSStrategicCategoryMapper.getCategoryList(parentId);
        return ls;
    }

    @Override
    public RestReturnResult getPage(SStrategicCategoryReqVO sStrategicCategoryReqVO) {
        RestReturnResult returnResult=RestReturnResult.success();
        PageCode pageCode = new PageCode(sStrategicCategoryReqVO.getPageNumber(), sStrategicCategoryReqVO.getPageSize());

        LicenseUtil.INSTANCE.validate();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("parentId", sStrategicCategoryReqVO.getSpecialityId());
        map.put("categoryId", sStrategicCategoryReqVO.getCategoryId());
        map.put("categoryName", sStrategicCategoryReqVO.getCategoryName());
        map.put("categoryStatus", sStrategicCategoryReqVO.getStatus());
        pageCode.setCount(extSStrategicCategoryMapper.getPageListCount(map));

        map.put("currentRecordNum", (pageCode.getPageNo() - 1) * pageCode.getPageSize());
        map.put("pageSize", pageCode.getPageSize());
        map.put("pageFlag", sStrategicCategoryReqVO.getPageFlag());

        List<ExtSStrategicCategory> ls = extSStrategicCategoryMapper.getPageList(map);
        pageCode.setList(ls);
        returnResult.setData(pageCode);
        return returnResult;
    }

    @Override
    public Map<String, String> findIdAndNames() {
        List<SStrategicCategory> list = selectList(null);
        if(list!=null&&list.size()>0){
            Map<String,String> map = Maps.newHashMapWithExpectedSize(list.size());
            for(SStrategicCategory sStrategicCategory:list){
                map.put(sStrategicCategory.getId(),sStrategicCategory.getCategoryName());
            }
            LicenseUtil.INSTANCE.validate();
            return map;
        }
        return null;
    }
}
